<?php
namespace App\Http\Controllers\Business\ShopWeb\FinanceManagement;

use App\Models\ShopUser;
use App\Models\SmsMessage;
use App\Models\Order;
use App\Models\Shop;
use App\Models\ShopAccount;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use App\Models\ShopSalesmanWithdrawal;
use Illuminate\Support\Facades\Redis;

class FinanceManagement {
    //订单结款统计数据
    public static function getOrderStatistic($shopIds) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //订单结款总计
        $totalWithdrawal = Order::whereIn('shopid', $shopIds)->where('refundstatus', 0)->sum('accountamount');
        //待结款金额
        $waitWithdrawal = Order::whereIn('shopid', $shopIds)->where('refundstatus', 0)->where('accountstatus', '0')->sum('accountamount');
        //已结款金额
//        $alreadyWithdrawal = Order::whereIn('shopid', $shopIds)->where('refundstatus', 0)->where('status', '1')->sum('accountamount');
        $alreadyWithdrawal = ShopAccount::whereIn('shopid',$shopIds)->sum('accountamount');
        $data = [
            'totalwithdrawal' => $totalWithdrawal,
            'waitwithdrawal' => $waitWithdrawal,
            'alreadywithdrawal' => $alreadyWithdrawal
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //订单列表
    public static function orderWithdrawalList($shopIds, $startTime='', $lastTime='', $page=1, $paginate=10) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        $skip = ($page-1)*$paginate;
        $lists = ShopAccount::whereIn('shop_accounts.shopid', $shopIds)->where('shop_accounts.status', '1')
            ->where('shop_accounts.created_at', '>=', $startTime)->where('shop_accounts.created_at', '<=', $lastTime)
            ->join('shops', 'shop_accounts.shopid', '=', 'shops.id')
            ->orderBy('shop_accounts.created_at', 'desc')
            ->select('shop_accounts.id as shopaccountid', 'shop_accounts.accounttime', 'shop_accounts.accountamount', 'shops.name as shopname')
            ->skip($skip)->take($paginate)->get();
        $count = ShopAccount::whereIn('shopid', $shopIds)->where('status', '1')
            ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
            ->get()->count();
        $sum = ShopAccount::whereIn('shopid', $shopIds)->where('status', '1')
            ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
            ->sum('accountamount');
        if(!$sum) {
            $sum = 0;
        }
        $data = [
            'lists' => $lists,
            'count' => $count,
            'sum' => $sum,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推结款统计数据
    public static function getSalesmanStatistic($shopIds) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //订单结款总计
        $totalWithdrawal = ShopSalesmanWithdrawal::whereIn('shopid', $shopIds)->sum('accountamount');
        //待结款金额
        $waitWithdrawal = ShopSalesmanWithdrawal::whereIn('shopid', $shopIds)->where('status', '0')->sum('accountamount');
        //已结款金额
        $alreadyWithdrawal = ShopSalesmanWithdrawal::whereIn('shopid', $shopIds)->where('status', '1')->sum('accountamount');
        $data = [
            'totalwithdrawal' => $totalWithdrawal,
            'waitwithdrawal' => $waitWithdrawal,
            'alreadywithdrawal' => $alreadyWithdrawal
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推结款列表
    public static function salesmanList($shopIds, $startTime='', $lastTime='', $page=1, $paginate=10) {
        if(empty($shopIds)) {
           return ['status'=>'failed', 'message'=>'参数不对'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        $skip = ($page-1) * $paginate;
        $lists = ShopSalesmanWithdrawal::whereIn('shop_salesman_withdrawal.shopid', $shopIds)->where('shop_salesman_withdrawal.status', 1)
            ->where('shop_salesman_withdrawal.updated_at', '>=', $startTime)->where('shop_salesman_withdrawal.updated_at', '<=', $lastTime)
            ->join('shops', 'shop_salesman_withdrawal.shopid', '=', 'shops.id')
            ->orderBy('shop_salesman_withdrawal.created_at', 'desc')
            ->select('shop_salesman_withdrawal.id as shopsalesmanid','shop_salesman_withdrawal.shopid',
                'shop_salesman_withdrawal.accounttime', 'shop_salesman_withdrawal.accountamount', 'shop_salesman_withdrawal.remark',
                'shops.name as shopname')
            ->skip($skip)->take($paginate)->get();
        $count = ShopSalesmanWithdrawal::whereIn('shopid', $shopIds)->where('status', 1)
            ->where('updated_at', '>=', $startTime)->where('updated_at', '<=', $lastTime)
            ->get()->count();
        $sum = ShopSalesmanWithdrawal::whereIn('shopid', $shopIds)->where('status', 1)
            ->where('updated_at', '>=', $startTime)->where('updated_at', '<=', $lastTime)
            ->sum('accountamount');
        if(!$sum) {
            $sum = 0;
        }
        $data = [
            'count' => $count,
            'sum' => $sum,
            'lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推结款详情
    public static function getSalesmanDetail($shopIds, $salesmanId) {
        if(!$salesmanId||!$shopIds) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopSalesmanWithdrawal = ShopSalesmanWithdrawal::where('id', $salesmanId)->whereIn('shopid', $shopIds)
            ->select('accounttime', 'registernum', 'validordernum', 'accountamount')
            ->first();
        if(!$shopSalesmanWithdrawal) {
            return ['status'=>'failed', 'message'=>'获取信息错误'];
        }
        $accountTime = $shopSalesmanWithdrawal['accounttime'];
        if($accountTime == '0000-00-00 00:00:00') {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $yearMonth = date('Y-m', strtotime($accountTime));
        $firstDay = date('Y-m-01', strtotime($yearMonth));
        $lastDay = date('Y-m-d', strtotime("$yearMonth +1 month -1 day"));
        $lists = ShopSalesmanDetail::whereIn('shopid', $shopIds)->where('getmoney', '<>', 0.00)
            ->where('created_at', '>=', $firstDay)->where('created_at', '<=', $lastDay)
            ->orderBy('created_at', 'desc')
            ->select('mobile', 'type', 'ordernumber', 'orderamount', 'getmoney', 'created_at')
            ->get();
        foreach($lists as $k=>$v) {
            $lists[$k]['mobile'] = str_replace(substr($lists[$k]['mobile'], 3, 4), '****', $lists[$k]['mobile']);
        }
        $count = count($lists);
        $registerNum = ShopSalesmanDetail::whereIn('shopid', $shopIds)->where('getmoney', '<>', 0.00)->where('type', 'register')
            ->where('created_at', '>=', $firstDay)->where('created_at', '<=', $lastDay)->count();
        $orderAmount = ShopSalesmanDetail::whereIn('shopid', $shopIds)->where('getmoney', '<>', 0.00)->where('type', 'order')
            ->where('created_at', '>=', $firstDay)->where('created_at', '<=', $lastDay)->sum('orderamount');
        $orderAmount = ($orderAmount != '')?$orderAmount:0.00;
        $getMoney = ShopSalesmanDetail::whereIn('shopid', $shopIds)->where('getmoney', '<>', 0.00)
            ->where('created_at', '>=', $firstDay)->where('created_at', '<=', $lastDay)->sum('getmoney');
        $getMoney = ($getMoney != '') ? $getMoney : 0.00;
        $data = [
            'lists' => $lists,
            'total' => $count,
            'registernum' => $registerNum,
            'orderamount' => $orderAmount,
            'getmoney' => $getMoney
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}